package com.css.fxfzcpxx.base.log;


import com.css.fxfzcpxx.async.service.AsyncService;
import com.css.fxfzcpxx.base.user.service.UserService;
import com.css.fxfzcpxx.base.user.viewobjects.UserBaseInfoVO;
import com.css.fxfzcpxx.modules.informationDelivery.param.ClaCasParams;
import com.css.fxfzcpxx.modules.logRecord.entity.SysLog;
import com.css.fxfzcpxx.modules.logRecord.repository.LogRecordRepository;
import com.css.fxfzcpxx.modules.thematicMap.param.QueParams;
import com.css.fxfzcpxx.util.PlatformDateUtils;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import com.css.fxfzcpxx.util.RedisLockUtil;
import com.css.fxfzcpxx.util.UUIDGenerator;
import com.css.fxfzcpxx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class AopLog {


    @Resource
    AsyncService asyncService;
    // 声明切点
    @Pointcut("@annotation(com.css.fxfzcpxx.base.log.Log)")
    public void logPointCut() {
    }

    /**
     * 环绕通知 @Around  ， 当然也可以使用 @Before (前置通知)  @After (后置通知)
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //多次执行会调用被代理方法多次
        Object result = point.proceed();
        MethodSignature signature = (MethodSignature) point.getSignature();
        try {
            asyncService.saveLog(point);
        } catch (Exception e) {
            log.info("aop日志错误,原因为:"+e.toString());
        }
        return result;
    }


    /**
     * 记录浏览量
     *
     * @param
     *//*
    private void saveLog(ProceedingJoinPoint point) throws Throwable {
        String requestId = UUIDGenerator.getUUID();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();

        SysLog sysLog = new SysLog();
        Log LogRecord = method.getAnnotation(Log.class);
        Boolean isLocked = redisLockUtil.lock("redis_eventCode_lock_serialNum", requestId, 6L);
        if (isLocked){
            log.info("成功获取redis_eventCode_lock_serialNum锁");
            if (LogRecord != null) {
                String releaseColumn = "";
                //类型
                String type = LogRecord.type();
                //模块
                String module = LogRecord.value();
                // 获取方法上的GetMapping注解
                GetMapping requestMapping = method.getAnnotation(GetMapping.class);
                // 获取接口路径
                String interfacePath = requestMapping.value()[0];
                *//*if("/claCasPage".equals(interfacePath)){
                    ClaCasParams args = (ClaCasParams) point.getArgs()[0];
                    //栏目
                    releaseColumn=args.getReleaseColumn();
                    if("典型案例".equals(releaseColumn)){
                        sysLog.setModules(releaseColumn);
                        module=releaseColumn;
                    }
                }*//*
                SysLog data=logRecordRepository.findByModuleAndType(module,type);
                if(PlatformObjectUtils.isNotEmpty(data)){
                    logRecordRepository.updatePageView(data.getLogId(),data.getPageView()+1);
                }else {
                    sysLog.setLogId(UUIDGenerator.getUUID());
                    sysLog.setOpType(type);
                    sysLog.setModules(module);
                    sysLog.setOpTime(PlatformDateUtils.getCurrentTimestamp());
                    sysLog.setPageView(1);
                    sysLog.setSign("0");
                    if(PlatformObjectUtils.isNotEmpty(PlatformSessionContext.getUserID())){
                        UserBaseInfoVO user = userService.getUser(PlatformSessionContext.getUserID());
                        if(PlatformObjectUtils.isNotEmpty(user)){
                            sysLog.setOpUser(user.getUserName());
                        }
                    }
                    logRecordRepository.save(sysLog);
                }
                redisLockUtil.releaseLock("redis_distributed_lock_serialNum", requestId);
                log.info("成功释放redis_distributed_lock_serialNum锁");
            }
        }else {
            log.info("获取redis_eventCode_lock_serialNum锁失败，进行休眠");
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }





    }*/

}
